package com.zeng.createmd.restfulmodule.util;

import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;

import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class ZipUtil {

    public static void unpack(File descDir) {
        unpack(null, descDir);
    }

    public static void unpack(String zipFile, String descDir) {
        try (ZipArchiveInputStream inputStream = getZipFile(zipFile)) {
            File desc = new File(descDir);
            if (!desc.exists()) {
                desc.mkdirs();
            }
            ZipArchiveEntry entry = null;
            while ((entry = inputStream.getNextZipEntry()) != null) {
                if (entry.isDirectory()) {
                    File directory = new File(descDir, entry.getName());
                    directory.mkdirs();
                } else {
                    OutputStream os = null;
                    try {
                        os = new BufferedOutputStream(new FileOutputStream(new File(descDir, entry.getName())));
                        //输出文件路径信息
                        IOUtils.copy(inputStream, os);
                    } finally {
                        IOUtils.closeQuietly(os);
                    }
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void unpack(File zipFile, File descDir) {
        try (ZipArchiveInputStream inputStream = getZipFile(zipFile)) {
            if (!descDir.exists()) {
                descDir.mkdirs();
            }
            ZipArchiveEntry entry = null;
            while ((entry = inputStream.getNextZipEntry()) != null) {
                if (entry.isDirectory()) {
                    File directory = new File(descDir, entry.getName());
                    directory.mkdirs();
                } else {
                    OutputStream os = null;
                    try {
                        os = new BufferedOutputStream(new FileOutputStream(new File(descDir, entry.getName())));
                        //输出文件路径信息
                        IOUtils.copy(inputStream, os);
                    } finally {
                        IOUtils.closeQuietly(os);
                    }
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static ZipArchiveInputStream getZipFile(File zipFile) throws Exception {
        if (zipFile == null) {
            InputStream resourceAsStream = ZipUtil.class.getClassLoader().getResourceAsStream("zip/md-test.zip");
            if (resourceAsStream == null) {
                throw new RuntimeException("压缩文件md-test.zip不存在");
            }
            return new ZipArchiveInputStream(new BufferedInputStream(resourceAsStream));
        }
        return new ZipArchiveInputStream(new BufferedInputStream(new FileInputStream(zipFile)));
    }

    private static ZipArchiveInputStream getZipFile(String zipFileClassPath) throws Exception {
        if (zipFileClassPath != null) {
            InputStream resourceAsStream = ZipUtil.class.getClassLoader().getResourceAsStream(zipFileClassPath);
            return new ZipArchiveInputStream(new BufferedInputStream(resourceAsStream));
        } else {
            throw new RuntimeException("zipFileClassPath 不能为空");
        }
    }
}
